package com.bit.forum.exception;


import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    //处理自定义异常
    @ResponseBody
    @ExceptionHandler(value = ApplicationException.class)
    public AppResult handleApplicationException(ApplicationException e) {

        e.printStackTrace();
        log.error(e.getMessage());

        if(e.getErrorResult()!=null){
            return e.getErrorResult();
        }
        return AppResult.failed(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public AppResult exceptionHandle(Exception e) {
        e.printStackTrace();
        log.error(e.getMessage());
        if (e.getMessage()==null){
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());
    }


}
